package com.vtb.listener;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vtb.entity.QqOauth;
import com.vtb.entity.RrOauth;
import com.vtb.entity.SsOauth;
import com.vtb.servlet.UnCachException;
import com.vtb.thread.CountWriteThread;
import com.vtb.thread.QqReadThread;
import com.vtb.thread.QqTraverThread;
import com.vtb.thread.RrReadThread;
import com.vtb.thread.RrTraverThread;
import com.vtb.thread.SsTraverThread;
import com.vtb.thread.VtbWriteThread;

public class ThreadListener implements ServletContextListener {
	private static final Log log = LogFactory.getLog(ThreadListener.class);
	private BlockingQueue<Object> send_queue = new ArrayBlockingQueue<Object>(20);
	
	private BlockingQueue<RrOauth> traver_rrQueue = new LinkedBlockingDeque<RrOauth>();
	private BlockingQueue<QqOauth> traver_qqQueue = new LinkedBlockingDeque<QqOauth>();
	private BlockingQueue<SsOauth> traver_ssQueue = new LinkedBlockingDeque<SsOauth>();

	public void contextDestroyed(ServletContextEvent sce) {
		
	}
	public void contextInitialized(ServletContextEvent sce) {
		log.info("线程监听器启动！！！");
		Runnable rrTraveRunnable = new RrTraverThread(traver_rrQueue);
		Thread rrTrvaerThread = new Thread(rrTraveRunnable,"人人网遍历线程");
		rrTrvaerThread.setUncaughtExceptionHandler(new UnCachException(rrTraveRunnable));
		rrTrvaerThread.start();
		
		Runnable qqTraveRunnable = new QqTraverThread(traver_qqQueue);
		Thread qqTraverThread = new Thread(qqTraveRunnable,"腾讯微博遍历线程");
		qqTraverThread.setUncaughtExceptionHandler(new UnCachException(qqTraveRunnable));
		qqTraverThread.start();
		
		Runnable ssTraveRunnable = new SsTraverThread(traver_ssQueue);
		Thread ssTraverThread = new Thread(ssTraveRunnable,"新浪微博遍历线程");
		ssTraverThread.setUncaughtExceptionHandler(new UnCachException(ssTraveRunnable));
		ssTraverThread.start();
		
		Runnable countWriteRunnable = new CountWriteThread();
		Thread countWriteThread= new Thread(countWriteRunnable,"数量记录线程");
		countWriteThread.setUncaughtExceptionHandler(new UnCachException(countWriteRunnable));
		countWriteThread.start();

		//分别创建3个线程进行读工作
		for (int i = 0; i < 5; i++) {
			Runnable rrReadRunnable = new RrReadThread(send_queue,traver_rrQueue);
			Thread rrReadThread = new Thread(rrReadRunnable,"人人读线程"+i+i+i+i);
			rrReadThread.setUncaughtExceptionHandler(new UnCachException(rrReadRunnable));
			rrReadThread.start();
			
			Runnable qqReadRunnable = new QqReadThread(send_queue,traver_qqQueue);
			Thread qqReadThread = new Thread(qqReadRunnable,"QQ读线程"+i+i+i+i);
			qqReadThread.setUncaughtExceptionHandler(new UnCachException(qqReadRunnable));
			qqReadThread.start();
		}
	
		Runnable vtbWriterRunnable = new VtbWriteThread(send_queue);
		Thread vtbWriteThread = new Thread(vtbWriterRunnable,"VTB写线程0000");
		vtbWriteThread.setUncaughtExceptionHandler(new UnCachException(vtbWriterRunnable));
		vtbWriteThread.start();
		
	}

}
